package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.AbstractFrameworkService;
import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.exception.PauseException;
import com.fdb.efp.nls.service.vo.CtrLoanContVO;
import com.fdb.efp.nls.service.dao.QueryAccInfoTaskDao;
import com.fdb.efp.nls.service.domain.QueryAccInfoTask;
import com.fdb.efp.nls.service.facade.PICCSToBANCSService;
import com.fdb.efp.nls.service.vo.AccLoanRevSubVO;
import com.fdb.efp.nls.service.vo.QueryAccInfoTaskVO;
import com.fdb.efp.nls.service.vo.ResponseVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service("PICCSToBANCSService")
public class PICCSToBANCSServiceImpl extends AbstractFrameworkService implements PICCSToBANCSService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(PICCSToBANCSServiceImpl.class);
	@Autowired
	private QueryAccInfoTaskDao queryAccInfoTaskDao;

	@Override
	/** 获取贷款账号 30022000007 01 */
	public Map<String, Object> getAccountInfo(String bizSerno, String billNo) throws PauseException {
		return null;
	}

	/** 贷款放款 30021000001 01 */
	public ResponseVO coreToLoan(AccLoanRevSubVO accLoanRevSubVO, CtrLoanContVO ctrLoanContVO) {
		return null;
	}

	/* 贷款还款结清 30021000002 01 */
	@Override
	public ResponseVO coreToLoanSett(String loanAcctNo) {
		//
		return null;
	}

	/* 贷款零余额结清 30021000002 02 */
	@Override
	public ResponseVO coreToLoanBlnSett(String loanAcctNo) {
		return null;
	}

	@Override
	public ResponseVO queryLoanAcctNoByBillNo(String billNo) {
		return null;
	}

	@Override
	public boolean autoQueryAccBasicInfoFromCore() {
		logger.info("自动从核心查询台账基本信息定时任务开始......");
		List<QueryAccInfoTask> list = new ArrayList<QueryAccInfoTask>();

		QueryAccInfoTaskVO queryAccInfoTaskVo = new QueryAccInfoTaskVO();
//		queryAccInfoTaskVo.setStatus(XfConstant.HANDLE_STATUS_UNDO);
		queryAccInfoTaskVo.setPage(0);
		queryAccInfoTaskVo.setSize(100);
		List<QueryAccInfoTask> listUnDo = queryAccInfoTaskDao.queryQueryAccInfoTaskByConditionByPage(queryAccInfoTaskVo);
		if (Objects.nonNull(listUnDo)) {
			list.addAll(listUnDo);
		}
//		queryAccInfoTaskVo.setStatus(XfConstant.HANDLE_STATUS_DONE_FAIL);
		List<QueryAccInfoTask> listFail = queryAccInfoTaskDao.queryQueryAccInfoTaskByConditionByPage(queryAccInfoTaskVo);
		if (Objects.nonNull(listFail)) {
			list.addAll(listFail);
		}
		if (Objects.nonNull(list) && list.size() > 0) {
			// 将任务设置为处理中
			List<Long> ids = new ArrayList<>();
			for (QueryAccInfoTask task : list) {
				ids.add(task.getId());
			}
			logger.info("更新任务状态为处理中......");
//			queryAccInfoTaskDao.updateQueryAccInfoTaskStstusByIds(ids, XfConstant.HANDLE_STATUS_DOING);
			// 异步线程
			ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
			cachedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					// 处理任务
					doAutoQueryAccBasicInfoFromCore(list);

					// 将已处理的信息转移到历史表
//					queryAccInfoTaskDao.insertQueryAccInfoTaskHis(XfConstant.HANDLE_STATUS_DONE);

					// 删除已处理的信息
//					queryAccInfoTaskDao.deleteQueryAccInfoTaskByStatus(XfConstant.HANDLE_STATUS_DONE);

					logger.info("自动从核心查询台账基本信息定时任务结束......");
				}
			});
			cachedThreadPool.shutdown();// 释放资源（任务执行完后才会释放）
		} else {
			logger.info("没有需要从核心查询台账基本信息的任务，定时任务处理结束！");
		}
		return true;
	}

	/**
	 * 处理任务
	 * 
	 * @param list
	 */
	private void doAutoQueryAccBasicInfoFromCore(List<QueryAccInfoTask> list) {
	}
}
